package com.offcn.service;

import com.offcn.bean.Blog;
import com.offcn.bean.BlogExample;
import com.offcn.bean.Btype;
import com.offcn.bean.BtypeExample;
import com.offcn.mapper.BlogMapper;
import com.offcn.mapper.BtypeMapper;
import com.offcn.utils.ReturnResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BtypeServiceImpl implements BtypeService {
    @Autowired
    private BtypeMapper btypeMapper;
    @Autowired
    private BlogMapper blogMapper;

    @Override
    public List<Btype> getBigType() {
        BtypeExample btypeExample = new BtypeExample();
        btypeExample.createCriteria().andTypePidIsNull();
        List<Btype> btypes = btypeMapper.getBigType(btypeExample);
        return btypes;
    }

    @Override
    public List<Btype> getsType(Integer typeId) {
        BtypeExample btypeExample = new BtypeExample();
        btypeExample.createCriteria().andTypePidEqualTo(typeId);
        List<Btype> sbtypes = btypeMapper.selectByExample(btypeExample);
        return sbtypes;
    }

    @Override
    public void addBtype(Btype btype) {
        btypeMapper.insertSelective(btype);
    }

    @Override
    public Btype getBtypeById(Integer typeid) {
        return btypeMapper.selectByPrimaryKey(typeid);
    }

    @Override
    public void updateBtype(Btype btype) {
        btypeMapper.updateByPrimaryKeySelective(btype);
    }

    @Override
    public ReturnResult deleteOneBtype(Integer typeid) {
        //根据id查询出该一级类别的名字
        Btype btype = btypeMapper.selectByPrimaryKey(typeid);

        BtypeExample btypeExample = new BtypeExample();
        btypeExample.createCriteria().andTypePidEqualTo(typeid);
        List<Btype> btypes = btypeMapper.selectByExample(btypeExample);
        if(btypes !=null && btypes.size()>0){
            //该一级类别下关联的有二级类别 不能删
            return new ReturnResult("【"+btype.getTypename()+"】下有二级类别，不能直接删除！",-1,null);
        }else{
            //一级类别下是空的
            try {
                btypeMapper.deleteByPrimaryKey(typeid);
                return new ReturnResult("删除成功!",1,null);
            } catch (Exception e) {
                e.printStackTrace();
                return new ReturnResult("删除失败!",-1,null);
            }
        }
    }

    @Override
    public ReturnResult deleteTwoBtype(Integer typeid) {
        //根据typeid查询该二级类别的名字
        Btype btype = btypeMapper.selectByPrimaryKey(typeid);

        //查询blog表有没有博客的type_fk等于typeid的
        BlogExample blogExample = new BlogExample();
        blogExample.createCriteria().andTypeFkEqualTo(typeid);
        List<Blog> blogs = blogMapper.selectByExample(blogExample);

        if(blogs != null && blogs.size()>0){
            //关联的有数据 不能删
            return new ReturnResult("【"+btype.getTypename()+"】下有博客，不能删除!",-1,null);
        }else{
            //可以删
            try {
                btypeMapper.deleteByPrimaryKey(typeid);
                return new ReturnResult("删除成功!",1,null);
            } catch (Exception e) {
                e.printStackTrace();
                return new ReturnResult("删除失败!",-1,null);
            }
        }
    }

}
